<!DOCTYPE html>
<html>
    <head>
        <title>AGM经销商设置</title>
        <link rel="stylesheet" href="/static/weui.min.css">
        <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
        <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">
        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style>
            .page, body{background-color: #fbf9fe}
            .page_title{color:#225fba}
            .hd{text-align:center}
            .weui_label{width:4em;}
            .bd{padding-bottom:30px;}
            .weui_cell_hd img{height: 40px;}
            .floatright{float:right}
            .nav_right{position: absolute;right: 5px;width: 35px;top: 5px;}
            .weui_actionsheet_menu a .weui_actionsheet_cell{color:initial;border-top: 1px solid #d9d9d9;}            
            .weui_cells.weui_cells_access.dealer{margin-top:0;}
            .extend_rule .weui_cell_bd, .rule .weui_cell_bd{-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;}
            .extend_rule .weui_cell_hd, .rule .weui_cell_hd{-webkit-box-flex: 1;-webkit-flex: 1;-ms-flex: 1;flex: 1;width:5em;}
            a.delrule:visited{
                color:inherit
            }
            a{ color:inherit; }
        </style>
    </head>
    <body>
        <div id='wx_logo' style="display:none;"><img src="/static/images/wx_logo.jpg"/></div>
        <div class="hd">
            <h2 class="page_title">AGM经销商设置</h2>
            <img src="/static/images/icon_nav_cell.png" class="nav_right">            
        </div>
        <div class="bd">
            <div class="weui_cells_title">默认设置</div>
            <div class="weui_cells weui_cells_access" id="defaultrule">
                <a class="weui_cell clickcheck" href="javascript:;">
                    <div class="weui_cell_bd weui_cell_primary">
                        <p>默认奖励规则设置</p>
                    </div>
                    <div class="weui_cell_ft">点击查看</div>                    
                </a>
                {% for key in sorted(dealerconfig['defaultrule'])%}
                <div class="weui_cell rulehide rule" style="display:none;">
                    <div class="weui_cell_hd">{{key}}</div>
                    <div class="weui_cell_bd">
                        <input class="weui_input" type="number" pattern="[0-9\.]*" step="0.01" value="{{dealerconfig['defaultrule'][key]}}" placeholder="请输入奖励值">
                    </div>
                    <a class="delrule" href="javascript:;"><i class="weui_icon_cancel"></i>删除</a>
                </div>
                {% endfor %}
                <div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addrule">添加规则</button></div>
                <a class="weui_cell clickcheck" href="javascript:;">
                    <div class="weui_cell_bd weui_cell_primary">
                        <p>默认额外奖励系数设置</p>
                    </div>
                    <div class="weui_cell_ft">点击查看</div>                    
                </a>
                {% for key in sorted(dealerconfig['defaultextrule']) %}
                <div class="weui_cell rulehide extend_rule" style="display:none;">
                    <div class="weui_cell_hd">达到<span class="extend_rule_key">{{key}}</span></div>
                    <div class="weui_cell_bd">
                        <input class="weui_input" type="number" pattern="[0-9\.]*" step="0.01" value="{{dealerconfig['defaultextrule'][key]}}" placeholder="请输入相应系数">
                    </div>
                    <a class="delrule" href="javascript:;"><i class="weui_icon_cancel"></i>删除</a>
                </div>
                {% endfor %}
                <div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addextrule">添加系数</button></div>
            </div>         
            <span id="dealers">
            <div class="weui_cells_title">经销商设置<button class="weui_btn weui_btn_mini weui_btn_primary floatright" id="adddealerbtn">添加经销商</button></div>
                {% for dealercode in dealerconfig['dealercodes'] %}
                <div class="weui_cells weui_cells_access dealer" dealercode="{{dealercode}}" dealername="{{dealerconfig['dealers'][dealercode]['name']}}">
                    <a class="weui_cell clickcheck" href="javascript:;">
                        <div class="weui_cell_bd weui_cell_primary">
                            <p>{{dealercode}}-{{dealerconfig['dealers'][dealercode]['name']}}</p>
                        </div>
                        <div class="weui_cell_ft">点击查看</div>                    
                    </a>
                    {% if not dealerconfig['dealers'][dealercode]['rule']%}
                    <div class="weui_cell rulehide norule" style="display:none;">
                        <div class="weui_cell_hd">当前经销商使用默认奖励规则</div>
                    </div>
                    {% else %}
                    {% for key in sorted(dealerconfig['dealers'][dealercode]['rule']) %}
                    <div class="weui_cell rule rulehide" style="display:none;">
                        <div class="weui_cell_hd">{{key}}</div>
                        <div class="weui_cell_bd">
                            <input class="weui_input" type="number" pattern="[0-9\.]*" step="0.01" value="{{dealerconfig['dealers'][dealercode]['rule'][key]}}" placeholder="请输入奖励值">
                        </div>
                        <a class="delrule" href="javascript:;"><i class="weui_icon_cancel"></i>删除</a>
                    </div>                
                    {% endfor %}
                    {% endif %}
                    <div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addrule">添加规则</button></div>
                    {% if not dealerconfig['dealers'][dealercode]['extrule'] %}
                    <div class="weui_cell rulehide noextrule" style="display:none;">
                        <div class="weui_cell_hd">当前经销商使用默认额外奖励系数</div>
                    </div>
                    {% else %}
                    {% for key in sorted(dealerconfig['dealers'][dealercode]['extrule']) %}
                    <div class="weui_cell rulehide extend_rule" style="display:none;">
                        <div class="weui_cell_hd">达到<span class="extend_rule_key">{{key}}</span></div>
                        <div class="weui_cell_bd">
                            <input class="weui_input" type="number" pattern="[0-9\.]*" step="0.01" value="{{ dealerconfig['dealers'][dealercode]['extrule'][key] }}" placeholder="请输入相应系数">
                        </div>
                        <a class="delrule" href="javascript:;"><i class="weui_icon_cancel"></i>删除</a>
                    </div>
                    {% endfor %}
                    {% endif %}
                    <div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addextrule">添加系数</button></div>                    
                    <div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_warn deldealer">删除经销商</button></div>
                </div>
                {% endfor %}
            </span>
            <div class="weui_cell"><button class="weui_btn weui_btn_primary" id="modifybtn">保存设置</button></div>
            <div class="weui_cell"><button class="weui_btn weui_btn_default" onclick="location.reload()">刷新设置</button></div>
        </div>             
        <div class="weui_dialog_confirm" id="dialog1" style="display:none">
            <div class="weui_mask"></div>
            <div class="weui_dialog">
                <div class="weui_dialog_hd"><strong class="weui_dialog_title">弹窗标题</strong></div>
                <div class="weui_dialog_bd">自定义弹窗内容，居左对齐显示，告知需要确认的信息等</div>
                <div class="weui_dialog_ft">
                    <a class="weui_btn_dialog default">取消</a>
                    <a class="weui_btn_dialog primary">确定</a>
                </div>
            </div>
        </div>        
        <div class="weui_dialog_alert" id="dialog2" style="display:none">
            <div class="weui_mask"></div>
            <div class="weui_dialog">
                <div class="weui_dialog_hd"><strong class="weui_dialog_title">弹窗标题</strong></div>
                <div class="weui_dialog_bd">弹窗内容，告知当前页面信息等</div>
                <div class="weui_dialog_ft">
                    <a class="weui_btn_dialog primary">确定</a>
                </div>
            </div>
        </div>
        <div class="weui_dialog_confirm" id="dialog1" style="display:none">
            <div class="weui_mask"></div>
            <div class="weui_dialog">
                <div class="weui_dialog_hd"><strong class="weui_dialog_title">弹窗标题</strong></div>
                <div class="weui_dialog_bd">自定义弹窗内容，居左对齐显示，告知需要确认的信息等</div>
                <div class="weui_dialog_ft">
                    <a class="weui_btn_dialog default">取消</a>
                    <a class="weui_btn_dialog primary">确定</a>
                </div>
            </div>
        </div> 
        <div id="actionSheet_wrap" style="display: none;">
            <div class="weui_mask_transition" id="mask" style="display: none;"></div>
            <div class="weui_actionsheet" id="weui_actionsheet">
                <div class="weui_actionsheet_menu">
                    <a href="sellerrank"><div class="weui_actionsheet_cell">销售排行</div></a>
                    <a href="sellerlist"><div class="weui_actionsheet_cell">销售列表</div></a>
                    <a href="dealersetting"><div class="weui_actionsheet_cell">经销商设置</div></a>
                </div>
                <div class="weui_actionsheet_action">
                    <div class="weui_actionsheet_cell" id="actionsheet_cancel">取消</div>
                </div>
            </div>
        </div>             
    </body>    
    <script>
        dialog2 = function(title, content, sucfunc){
            var self = $('#dialog2')
            self.find('.weui_dialog_title').text(title)
            self.find('.weui_dialog_bd').html(content)
            var clickfunc = sucfunc?sucfunc:function(){self.hide();}
            self.find('.weui_btn_dialog.primary').click(clickfunc)
            self.show()
        }
        $(document).on('click', '.clickcheck', function(){
            var self = $(this)
            if(self.parents('#dealers').length!=0){
                self.parent().siblings('.dealer').hide()
            }
            self.find('.weui_cell_ft').text('点击收起')
            self.nextUntil('.clickcheck,.clickhiden', '.rulehide').show()
            self.removeClass('clickcheck')
            self.addClass('clickhiden')
        })
        $(document).on('click', '.clickhiden', function(){
            var self = $(this)
            if(self.parents('#dealers').length!=0){
                self.parent().siblings('.dealer').show()
            }            
            self.find('.weui_cell_ft').text('点击查看')            
            self.nextUntil('.clickcheck,.clickhiden', '.rulehide').hide()
            self.addClass('clickcheck')
            self.removeClass('clickhiden')
        }) 
        $(document).on('click', '#modifybtn', function(){
            data = {}
            var rules = getrules($('#defaultrule'))
            data['defaultrule'] = rules[0]
            data['defaultextrule'] = rules[1]
            data['dealers'] = {}
            data['dealercodes'] = []
            $('.dealer').each(function(){
                var dealercode = $(this).attr('dealercode')
                var dealername = $(this).attr('dealername')
                var rules = getrules($(this))
                data['dealercodes'].push(dealercode)
                data['dealers'][dealercode] = {'name': dealername, 'rule': {}}
                data['dealers'][dealercode]['rule'] = rules[0]
                data['dealers'][dealercode]['extrule'] = rules[1]
            })
            $.post('manage?action=modifydealersetting', {'dealerconfig': JSON.stringify(data)}).done(function(data){
                console.info(data)
                if(data=='modifysucess'){
                    location.reload();
                }else if(data=='inputfaild'){
                    alert('输入有误，规则设置值须为整数，请重新查看后再保存设置')
                }
            })
        })
        getrules = function(e){
            var rule = {}
            var extrule = {}
            e.find('.rule').each(function(){
                var key = $(this).find('.weui_cell_hd').text()
                var value = $(this).find('.weui_input').val()
                console.info(key, value)
                rule[key] = value
            })
            e.find('.extend_rule').each(function(){
                var key = $(this).find('.extend_rule_key').text()
                var value = $(this).find('.weui_input').val()
                console.info(key, value)
                extrule[key] = value
            })
            return [rule, extrule]
        }
        $('#adddealerbtn').click(function(){
            var sucfunc = function(){
                var dealercode = $('#inputdealercode').val()
                var dealername = $('#inputdealername').val()
                $('#dealers').append(
                '<div class="weui_cells weui_cells_access dealer" dealercode="'+dealercode+'" dealername="'+dealername+'">'
                    +'<a class="weui_cell clickcheck" href="javascript:;">'
                        +'<div class="weui_cell_bd weui_cell_primary">'
                            +'<p>'+dealercode+'-'+dealername+'</p>'
                        +'</div>'
                        +'<div class="weui_cell_ft">点击查看</div>'                    
                    +'</a>'
                    +'<div class="weui_cell rulehide norule" style="display:none;"><div class="weui_cell_hd">当前经销商使用默认奖励规则</div></div>'
                    +'<div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addrule">添加规则</button></div>'
                    +'<div class="weui_cell rulehide noextrule" style="display:none;"><div class="weui_cell_hd">当前经销商使用默认额外奖励系数</div></div>'
                    +'<div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_primary addextrule">添加系数</button></div> '
                    +'<div class="weui_cell rulehide" style="display:none;"><button class="weui_btn weui_btn_warn deldealer">删除经销商</button></div>'
                +'</div>'                
                )
                $('#dialog1').hide()
            }
            myinputdailog('请输入添加经销商信息', '<div class="weui_cells weui_cells_form "><div class="weui_cell"><div class="weui_cell_hd">代码</div><div class="weui_cell_bd weui_cell_primary"><input id="inputdealercode" class="weui_input" placeholder="请输入经销商代码"></div></div><div class="weui_cell"><div class="weui_cell_hd">名称</div><div class="weui_cell_bd weui_cell_primary"><input id="inputdealername" class="weui_input" placeholder="请输入经销商名称"></div></div></div><div class="weui_cells_tips">注意：未添加规则与系数前，该经销商奖励规则使用默认设置</div>',sucfunc)
        })
        myinputdailog = function(title, content, sucfunc){
            var dailog = $('#dialog1')
            dailog.find('.weui_dialog_title').text(title)
            dailog.find('.weui_dialog_bd').html(content)
            dailog.find('.weui_btn_dialog.default').click(function(){
                dailog.hide()
            })
            dailog.find('.weui_btn_dialog.primary').unbind('click').click(sucfunc)        
            dailog.show()    
        }
        $(document).on('click', '.addrule,.addextrule', function(){
            var self = $(this)
            var msg = ['奖励值', '规则', '产品型号', '型号:如TR-01', '奖励值', '相应奖励值', 'rule', '', '', '']
            var isext = self.hasClass('addextrule')?true:false
            if(isext){
                msg = ['相应系数','额外系数', '销售数量', '数量:如10', '奖励系数', '相应奖励系数', 'extend_rule', '达到<span class="extend_rule_key">','</span>', ' type="number" pattern="[0-9\.]*" step="0.01"']
            }
            var sucfunc = function(){
                var pname = isext?$('#inputpname').val():$('#inputpname').val().toUpperCase()
                if(!pname){$('#dialog1').hide();return}
                var reward = $('#inputreward').val()
                self.parent().before(
                '<div class="weui_cell rulehide '+msg[6]+'">'
                    +'<div class="weui_cell_hd">'+msg[7]+pname+msg[8]+'</div>'
                    +'<div class="weui_cell_bd">'
                        +'<input class="weui_input" type="number" pattern="[0-9\.]*" step="0.01" value="'+reward+'" placeholder="'+msg[0]+'">'
                    +'</div>'
                    +'<a class="delrule" href="javascript:;"><i class="weui_icon_cancel"></i>删除</a>'
                +'</div>'                
                )
                self.parent().prevAll('.'+(isext?'noextrule':'norule')).remove()
                $('#dialog1').hide()
            }
            myinputdailog('请输入要添加的'+msg[1]+'','<div class="weui_cells weui_cells_form "><div class="weui_cell"><div class="weui_cell_hd">'+msg[2]+'</div><div class="weui_cell_bd weui_cell_primary"><input id="inputpname"'+msg[9]+' class="weui_input" placeholder="'+msg[3]+'"></div></div><div class="weui_cell"><div class="weui_cell_hd">'+msg[4]+'</div><div class="weui_cell_bd weui_cell_primary"><input id="inputreward" type="number" pattern="[0-9\.]*" step="0.01" class="weui_input" placeholder="'+msg[5]+'"></div></div></div>', sucfunc)
        })
        $(document).on('click', 'a.delrule', function(){
            var self = $(this)
            var sucfunc = function(){
                self.parent().remove()
                $('#dialog1').hide()
            }
            myinputdailog('删除操作确认', '<div style="text-align:center;"><i class="weui_icon_msg weui_icon_warn"></i><br/>当前操作为删除奖励规则<br/>点确定键确认操作</div>', sucfunc)
        })
        $(document).on('click', '.deldealer', function(){
            var self = $(this)
            var sucfunc = function(){
                var parent = self.parents('.dealer')
                parent.siblings('.dealer').show()
                parent.remove()
                $('#dialog1').hide()
            }
            myinputdailog('删除操作确认', '<div style="text-align:center;"><i class="weui_icon_msg weui_icon_warn"></i><br/>当前操作为删除经销商<br/>点确定键确认操作</div>', sucfunc)
        })
        $('.nav_right').click(function(){
            $('#actionSheet_wrap').show()
            var mask = $('#mask');
            var weuiActionsheet = $('#weui_actionsheet');
            weuiActionsheet.addClass('weui_actionsheet_toggle');
            mask.show().addClass('weui_fade_toggle').one('click', function () {
                hideActionSheet(weuiActionsheet, mask);
            });
            $('#actionsheet_cancel').one('click', function () {
                hideActionSheet(weuiActionsheet, mask);
            });
            weuiActionsheet.unbind('transitionend').unbind('webkitTransitionEnd');

            function hideActionSheet(weuiActionsheet, mask) {
                weuiActionsheet.removeClass('weui_actionsheet_toggle');
                mask.removeClass('weui_fade_toggle');
                weuiActionsheet.on('transitionend', function () {
                    mask.hide();
                    $('#actionSheet_wrap').css('display', 'none')
                }).on('webkitTransitionEnd', function () {
                    mask.hide();
                    $('#actionSheet_wrap').css('display', 'none')
                })
            }
        })       
    </script>
<html>